<?php

namespace App\Libs;

use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;

class Blogger
{
    /**
     * 日志名称
     */
    const LOG_ERROR = 'error';
    const LOG_MODEL_ERROR = 'model-error';
    const LOG_GLOBAL_SUCCESS = 'global-success';

    const LOG_LOGIN_FAIL = 'login_fail';

    //实例
    private static $loggers = [];

    protected function __construct()
    {
        //disallow new instance
    }

    protected function __clone()
    {
        //disallow clone
    }

    /**
     * 获取实例
     * @param string $type
     * @return mixed
     */
    public static function getLogger($type = self::LOG_ERROR)
    {
        $logPath = app()->storagePath() . '/logs/';
        if (empty(self::$loggers[$type])) {
            self::$loggers[$type] = new Logger($type);
            $handler = new RotatingFileHandler($logPath . $type . '.log', 10, Logger::INFO, true, 0777);
            $handler->setFormatter(new LineFormatter(null, "Y-m-d H:i:s", true, true));
            self::$loggers[$type]->pushHandler($handler);

        }
        return self::$loggers[$type];
    }
}
